/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "carina/renderer-base.hh"

#ifdef HAS_D3D
#include "carina/dx-library.hh"
#endif

#ifdef HAS_OPENGL
#include "carina/gl-library.hh"
#endif

namespace Carina
{
VideoBuffer::~VideoBuffer() {}
StateObject::~StateObject() {}
BlendStateObject::~BlendStateObject() {}
DepthStencilStateObject::~DepthStencilStateObject() {}
InputLayout::~InputLayout() {}

size_t SizeofVarType(VarType vt)
{
    switch(vt)
    {
    case VT_R32F: return 4;
    case VT_RG32F: return 8;
    case VT_RGB32F: return 12;
    case VT_RGBA32F: return 16;
    case VT_R16F: return 2;
    case VT_RG16F: return 4;
//  case VT_RGB16F: return 6;
    case VT_RGBA16F: return 8;
    case VT_R32: return 4;
    case VT_RG32: return 8;
    case VT_RGB32: return 12;
    case VT_RGBA32: return 16;
    case VT_R16: return 2;
    case VT_RG16: return 4;
//  case VT_RGB16: return 6;
    case VT_RGBA16: return 8;
    case VT_R8: return 1;
    case VT_RG8: return 2;
//  case VT_RGB8: return 3;
    case VT_RGBA8: return 4;
    case VT_uR32: return 4;
    case VT_uRG32: return 8;
    case VT_uRGB32: return 12;
    case VT_uRGBA32: return 16;
    case VT_uR16: return 2;
    case VT_uRG16: return 4;
//  case VT_uRGB16: return 6;
    case VT_uRGBA16: return 8;
    case VT_uR8: return 1;
    case VT_uRG8: return 2;
//  case VT_uRGB8: return 3;
    case VT_uRGBA8: return 4;
    
    case VT_R16_SNORM: return 2;
    case VT_RG16_SNORM: return 4;
//  case VT_RGB16_SNORM: return 6;
    case VT_RGBA16_SNORM: return 8;
    case VT_R8_SNORM: return 1;
    case VT_RG8_SNORM: return 2;
//  case VT_RGB8_SNORM: return 3;
    case VT_RGBA8_SNORM: return 4;
    case VT_R16_UNORM: return 2;
    case VT_RG16_UNORM: return 4;
//  case VT_RGB16_UNORM: return 6;
    case VT_RGBA16_UNORM: return 8;
    case VT_R8_UNORM: return 1;
    case VT_RG8_UNORM: return 2;
//  case VT_RGB8_UNORM: return 3;
    case VT_RGBA8_UNORM: return 4;

    case VT_R10G10B10A2: return 4;
    case VT_UINT_R10G10B10A2: return 4;
    default:
        return 0;
    }
}

RenderingLibrary::RenderingLibrary(const string& name)
    :   m_Name(name) {}

RenderingSubsystem::RenderingSubsystem()
{
#if defined(_WIN32) && defined(HAS_D3D)
    if(DXLibrary::check())
        m_Libraries.push_back(make_aligned_shared<DXLibrary>());
#endif
#ifdef HAS_OPENGL
    if(GLLibrary::check())
        m_Libraries.push_back(make_aligned_shared<GLLibrary>());
#endif
}

RenderingLibraryPtr RenderingSubsystem::getLibrary(size_t idx)
{
    return m_Libraries[idx];
}

RenderingLibraryPtr RenderingSubsystem::getLibrary(const string& name)
{
    for(size_t i = 0; i < m_Libraries.size(); ++i)
        if(m_Libraries[i]->getName() == name)
            return m_Libraries[i];
    return RenderingLibraryPtr();
}
}

